package cn.bluemobi.dylan.step.ui;

import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpActivity;
import cn.bluemobi.dylan.step.presenter.TabMainPresenter;
import cn.bluemobi.dylan.step.presenter.contract.TabMainContract;
import cn.bluemobi.dylan.step.ui.main.fragment.MainFragment;
import cn.bluemobi.dylan.step.ui.my.fragment.MyFragment;
import cn.bluemobi.dylan.step.view.MyFragmentTabHost;

public class TabMainActivity extends BaseMvpActivity<TabMainPresenter> implements TabMainContract.View {
    public static final int ADD_PATIENT = 1001;
    private static final String MAIN = "今日步数";
    private static final String MY = "我的";
    private MyFragmentTabHost MyTabHost;
    private Class mTabFragment[] = {MainFragment.class, MyFragment.class};
    private int picture[] = {R.drawable.tab_main_selector, R.drawable.tab_my_selector};
    private String tagName[] = {MAIN, MY};
    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.tv_title)
    TextView tv_title;


    @Override
    protected int getLayout() {
        return R.layout.tabmain;
    }

    @Override
    protected void initView() {
        super.initView();
        initToolbar();
        MyTabHost = findViewById(R.id.tabHost);
        MyTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent1);
        for (int i = 0; i < mTabFragment.length; i++) {
            TabWidget tabWidget = MyTabHost.getTabWidget();
            tabWidget.setDividerDrawable(null);
            MyTabHost.addTab(MyTabHost.newTabSpec(tagName[i]).setIndicator(getTabItemView(i)), mTabFragment[i], null);
        }

        MyTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String s) {
                if (MY.equals(s)) {
                    tv_title.setText(MY);
                } else if(MAIN.equals(s)){
                    tv_title.setText(MAIN);
                }
            }
        });

    }

    private void initToolbar() {
        //设置toolbar
        setSupportActionBar(mToolbar);
        toolbar_back.setVisibility(View.GONE);
        tv_title.setText("今日步数");
    }


    protected View getTabItemView(int index) {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.item_tab, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageViewTag);
        imageView.setImageResource(picture[index]);
        TextView textView = (TextView) view.findViewById(R.id.textViewTag);
        textView.setText(tagName[index]);
        return view;
    }


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    public void jumpToLogin() {

    }

    @Override
    public void jumpToMain() {

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private long mExitTime = 0;

    @Override
    public void onBackPressedSupport() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
        }

    }


    private static final int PERMISSION_REQUEST_CODE = 001;

    @Override
    protected void onStart() {
        super.onStart();

    }

    /**
     * 显示提示消息
     *
     * @param msg
     */
    private void showMessage(String msg) {
//        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void closeDialog() {

    }
}